package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.AccountInstagramActRdbService;
import com.ndp.fb.rdb.db.BatchPstAssign;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.AccountInstagramAct;
import com.ndp.fb.util.ListUtil;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Evan on 12/15/2015.
 */
@Service
public class AccountInstagramActRdbServiceImpl extends DaoTemplate implements AccountInstagramActRdbService {

    @Override
    public void saveList(final List<AccountInstagramAct> list)
    {
        String sql = "INSERT INTO fb_act_instagram_act " +
                "(fb_act_id, ins_act_id, ins_act_username, ins_act_owner_business_id, " +
                "ins_act_is_published, has_profile_picture, update_time, is_delete) " +
                "VALUES (?, ?, ?,?, ?, ?, ?,?)";
        batchSaveResultId(sql, new BatchPstAssign() {
            @Override
            public void setParam(PreparedStatement ps, Object o) throws SQLException {
                AccountInstagramAct pr = (AccountInstagramAct)o;
                ps.setObject(1, pr.getFbAccountId());
                ps.setObject(2, pr.getInsAccountId());
                ps.setObject(3, pr.getInsAccountUsername());
                ps.setObject(4, pr.getInsAccountOwnerBmId());
                ps.setObject(5, pr.getInsAccountIsPublished());
                ps.setObject(6, pr.getHasProfilePic());
                ps.setObject(7, new Date().getTime());
                ps.setObject(8, pr.getIsDelete());
            }
        }, list);
    }

    @Override
    public Long save(AccountInstagramAct pr)
    {
        return saveResultId(pr);
    }


    @Override
    public List<AccountInstagramAct> queryInsActByAccount(Long fbAccount) {
        String sql = "SELECT * FROM  fb_act_instagram_act  WHERE 1=1 AND fb_act_id =? AND has_profile_picture = 1 AND is_delete = 0";
        return getJdbcTemplate().query(sql,new Object[]{fbAccount}, DefaultRowMapper.autoRowMapper(AccountInstagramAct.class));
    }

    @Override
    public  List<AccountInstagramAct> findInsActIdsByAccountId(Long fbAccount)
    {
        String sql = "SELECT * FROM  fb_act_instagram_act  WHERE 1=1 AND fb_act_id =?";
        return getJdbcTemplate().query(sql, new Object[]{fbAccount}, DefaultRowMapper.autoRowMapper(AccountInstagramAct.class));
    }

    @Override
    public void updateIsDelete(List<Long> insIds, Long fbActId)
    {
        String sql = "UPDATE fb_act_instagram_act SET " +
                "is_delete = 1 WHERE 1=1 AND " +
                "fb_act_id = ? AND ins_act_id = ?";
        getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setObject(1, fbActId);
                ps.setObject(2, insIds.get(i));
            }

            @Override
            public int getBatchSize() {
                return insIds.size();
            }
        });
    }

    @Override
    public void batchSaveOrUpdate(List<AccountInstagramAct> list)
    {
        if(ListUtil.isEmpty(list))
            return;
        List<AccountInstagramAct> updateList = new ArrayList<>();
        List<AccountInstagramAct> insertList = new ArrayList<>();
        list.forEach(obj -> {
            if (obj != null && obj.getId() != null) {
                updateList.add(obj);
            } else {
                insertList.add(obj);
            }
        });
        if(ListUtil.isNotEmpty(updateList))
        {
            String sql = "UPDATE fb_act_instagram_act SET " +
                    "is_delete = 0, ins_act_username = ?, ins_act_owner_business_id = ?, ins_act_is_published =?, has_profile_picture =?, update_time=? WHERE 1=1 AND " +
                    "id = ?";
            getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    AccountInstagramAct obj = updateList.get(i);
                    ps.setObject(1, obj.getInsAccountUsername());
                    ps.setObject(2, obj.getInsAccountOwnerBmId());
                    ps.setObject(3, obj.getInsAccountIsPublished());
                    ps.setObject(4, obj.getHasProfilePic());
                    ps.setObject(5, new Date().getTime());
                    ps.setObject(6, obj.getId());
                }

                @Override
                public int getBatchSize() {
                    return updateList.size();
                }
            });
        }
        if(ListUtil.isNotEmpty(insertList))
        {
            saveList(insertList);
        }
    }
}
